package com.yupi.moonBI.mq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class TtlPonducer {

    public static final String QUEUE_NAME = "ttl_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        //1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.88.130");//ip 默认值localhost
        factory.setPort(5672);//设置端口
        //3、创建连接Connection
        try (
            Connection connection = factory.newConnection();
            //4、创建Channel
            Channel channel = connection.createChannel()){
            //5、创建队列Queue

            //String message = "hello rabbitmq~~~";
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNext()){
                //6、发送消息
                String message = scanner.nextLine();
                AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                        .expiration("20000")
                        .build();//设置消息的过期时间，单位是ms
                //如果使用的是默认的交换机，那路由名称必须要和队列名称相同
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
                System.out.println("[x] sent "+message);
            }

        }
    }
}
